<template>
  <div class="process-cell">
    <div v-if="false" class="process-label">
      <div class="process-icon">
        <el-icon :size="16">
          <component :is="iconComponent" />
        </el-icon>
      </div>
      <div class="process-text">{{ label }}</div>
    </div>
    <div class="process-content">
      <template v-for="(tag, idx) in selectedItem">
        <el-tag class="item-tag" closable size="large" type="info" @close="handleRemoveTag(idx)">{{ tag.text }}</el-tag>
        <span class="item-tag-icon">
          <el-icon><Right /></el-icon>
        </span>
      </template>
      <el-button class="item-btn" type="info" size="large" :icon="Plus" plain @click="dialogApproval?.openDialog()" />
    </div>
  </div>
  <DialogApproval ref="dialogApproval" v-model="selectedItem" @closed="handlerClosedDialogApproval" />
</template>
<script setup lang="ts">
import { computed, ref } from 'vue'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import { Plus, Right } from '@element-plus/icons-vue'
import DialogApproval from './DialogApproval.vue'

const emit = defineEmits(['update:modelValue'])

const dialogApproval = ref<InstanceType<typeof DialogApproval>>()
const props = defineProps({
  label: {
    type: String,
    default: '',
  },
  icon: {
    type: String,
    default: 'Position',
  },
  modelValue: {
    type: Array,
    default: () => [],
  },
})
const selectedItem = ref<any[]>(props.modelValue)
// 动态获取图标组件
const iconComponent = computed(() => {
  return ElementPlusIconsVue[props.icon as keyof typeof ElementPlusIconsVue]
})

// 关闭审批弹窗
const handlerClosedDialogApproval = () => {
  emit('update:modelValue', selectedItem.value)
}

const handleRemoveTag = (idx: number) => {
  selectedItem.value.splice(idx, 1)
}
</script>

<style lang="scss" scoped>
.process-cell {
  .process-label {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    line-height: 1;
    .process-icon {
      margin-right: 8px;
      font-size: 20px;
      color: #fff;
      background-color: var(--el-color-primary);
      border-radius: 50%;
      width: 32px;
      height: 32px;
      display: flex;
      align-items: center;
      justify-content: center;
    }

    .process-text {
      font-size: 14px;
    }
  }
  .process-content {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    .item-tag-icon {
      display: inline-block;
      height: 40px;
      padding: 0 8px;
    }
    .item-tag {
      margin-bottom: 12px;
      height: 40px;
      font-size: 14px;
      + .item-btn {
        margin-left: 12px;
      }
    }
    .item-btn {
      border-color: var(--el-color-info-light-8) !important;
    }
  }
}
</style>
